/*
 * @(#)ConnectorEdges.java   09/09/13
 *
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 *
 *
 *
 */



package fr.ema.arch3d.gea.ass;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.Interface;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.julia.control.binding.Util;
import org.objectweb.fractal.util.Fractal;

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class ConnectorEdges extends MultiEdge {

    /** Field description */
    Object[] connector = new Object[4];

    /** Field description */
    Component cc, cs;

    /** Field description */
    Component cc1, cs1;

    /** Field description */
    String client_itf, server_itf;

    /** Field description */
    String client_itf1, server_itf1;

    /** Field description */
    String name;

    /**
     * Constructs ...
     *
     *
     * @param connector
     */
    public ConnectorEdges(Component connector) {
        try {
            name = Fractal.getNameController(connector).getFcName();
        } catch (NoSuchInterfaceException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Interface pctr;

        try {
            Interface pcmp  = (Interface) Fractal.getBindingController(connector).lookupFc("r-I");
            Interface pcmp1 = (Interface) Fractal.getBindingController(connector).lookupFc("r-II");

            // System.out.println(name+"="+pcmp1.getFcItfName());
            pctr = (Interface) connector.getFcInterface("p");

            Object[] rcmp = Util.getFcClientItfsBoundTo(pctr).toArray();

            // System.out.println("eeeeeeeeeeeeeeeeee"+name);
            this.cs = pcmp.getFcItfOwner();
            this.cc = ((Interface) rcmp[0]).getFcItfOwner();

            for (int i = 0; i < rcmp.length; i++) {
                Component t = ((Interface) rcmp[i]).getFcItfOwner();

                // System.out.println(name+"="+"here==="+Fractal.getNameController(t).getFcName());
            }

            if (rcmp.length > 1) {
                this.cc1 = ((Interface) rcmp[1]).getFcItfOwner();

                // System.out.println("here==="+Fractal.getNameController(cc1).getFcName());
                this.client_itf1 = ((Interface) rcmp[1]).getFcItfName();
            }

            if (pcmp1 != null) {
                this.cs1         = pcmp1.getFcItfOwner();
                this.server_itf1 = pcmp1.getFcItfName();
            }

            this.client_itf = ((Interface) rcmp[0]).getFcItfName();
            this.server_itf = pcmp.getFcItfName();
        } catch (NoSuchInterfaceException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // System.out.println(cs1+"="+cc1);
    }

    /**
     * Constructs ...
     *
     *
     * @param connector
     * @param newCmpName
     */
    public ConnectorEdges(Component connector, String newCmpName) {
        try {
            name = Fractal.getNameController(connector).getFcName();
        } catch (NoSuchInterfaceException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Interface pctr;

        try {
            Interface pcmp  = (Interface) Fractal.getBindingController(connector).lookupFc("r-I");
            Interface pcmp1 = (Interface) Fractal.getBindingController(connector).lookupFc("r-II");

            // System.out.println(name+"="+pcmp1.getFcItfName());
            pctr = (Interface) connector.getFcInterface("p");

            Object[] rcmp = Util.getFcClientItfsBoundTo(pctr).toArray();

            this.cs = pcmp.getFcItfOwner();

            if (rcmp.length == 1) {
                this.cc = ((Interface) rcmp[0]).getFcItfOwner();
            } else {
                if (!Fractal.getNameController(((Interface) rcmp[0]).getFcItfOwner()).getFcName().equals(newCmpName)) {
                    this.cc          = ((Interface) rcmp[0]).getFcItfOwner();
                    this.cc1         = ((Interface) rcmp[1]).getFcItfOwner();
                    this.client_itf1 = ((Interface) rcmp[1]).getFcItfName();
                } else {
                    this.cc          = ((Interface) rcmp[1]).getFcItfOwner();
                    this.cc1         = ((Interface) rcmp[0]).getFcItfOwner();
                    this.client_itf1 = ((Interface) rcmp[0]).getFcItfName();
                }
            }

            if (pcmp1 != null) {
                this.cs1         = pcmp1.getFcItfOwner();
                this.server_itf1 = pcmp1.getFcItfName();
            }

            this.client_itf = ((Interface) rcmp[0]).getFcItfName();
            this.server_itf = pcmp.getFcItfName();
        } catch (NoSuchInterfaceException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // System.out.println(cs1+"="+cc1);
    }

    void setConnection(Component cc, String client_itf, Component cs, String server_itf) {

        /*
         * this.cc=cc;
         * this.cs=cs;
         * this.client_itf=client_itf;
         * this.server_itf=server_itf;
         */
    }

    Object[] getConnection() {
        connector[0] = this.cc;
        connector[1] = this.client_itf;
        connector[2] = this.cs;
        connector[3] = this.server_itf;

        return connector;
    }
}
